<?php 
try {
	include_once('db/constants.php');
	include_once('db/get_categories_db.php');
	include_once('db/error_log_db.php');
	include_once('facebook_login.php');

	$current_file = "uploadCategory";
	include_once("users_flow_logger.php");

} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="overflow-x: hidden">
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<title><?php echo HTML_TITLE;?></title>
	<link rel="stylesheet" href="css/style.css" type="text/css" media="all" />
	<!--[if lte IE 6]><link rel="stylesheet" href="css/ie6.css" type="text/css" media="all" /><![endif]-->
	
	<script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>
	<script src="js/spin.js" type="text/javascript"></script>
	<script src="js/wrap_content.js?<?php echo REVISION;?>" type="text/javascript"></script>

	<script>
		var submitting = false;
		var currentCategory = null;
		<?php 
			if ( isset($_REQUEST['category_id']) ) { 
				echo "currentCategory = JSON.parse(\"".addslashes(get_one_category($_REQUEST['category_id'], $user_profile['id']))."\");";
			}
		?>

		function submitCategory(){
			if (submitting == true)
				return;

			submitting = true;
			var name = capitaliseFirstLetter(document.getElementById("category_textarea").value.replace(/\n/g, "<br />"));
			
			spinner.spin(spinnerTarget);
			
			var callUrl = "db/upload_category_db.php?user_id=<?php echo $user_profile['id']; ?>&name=" + name + "&first_name=<?php echo urlencode($user_profile['first_name']); ?>&last_name=<?php echo urlencode($user_profile['last_name']); ?>";

			if (currentCategory != null) {
				callUrl = callUrl + "&category_id=<?php if ( isset($_REQUEST['category_id']) ) echo $_REQUEST['category_id']; ?>";
			}

			$.post(callUrl, {
			}, function(response){
				window.location.href="<?php echo CONTENTS_URL;?>?category_id=" + response;                             
			});

		}

		function capitaliseFirstLetter(string)
		{
		    return string.charAt(0).toUpperCase() + string.slice(1);
		}
	</script>
	
</head>
<body>
	
	<div id="fb-root"></div>
	<script>(function(d, s, id) {
	  var js, fjs = d.getElementsByTagName(s)[0];
	  if (d.getElementById(id)) return;
	  js = d.createElement(s); js.id = id;
	  js.src = "//connect.facebook.net/es_LA/all.js";
	  fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));</script>


    <script>		
	window.fbAsyncInit = function() {
	FB.init({
	  appId : '<?php echo APP_ID;?>',
	  xfbml : true,
	  version : 'v2.0'
	});
	};
	(function(d, s, id){
         var js, fjs = d.getElementsByTagName(s)[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement(s); js.id = id;
         js.src = "//connect.facebook.net/es_LA/sdk.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));
    </script>

<?php 
$headerFirstOption = MY_CATEGORIES;
$headerSecondOption = CATEGORIES;
$headerThirdOption = HOME;

$headerFirstUrl = PUBLISH_CATEGORY."?coming_from=".$current_file;
$headerSecondUrl = CATEGORIES_URL."?coming_from=".$current_file;
$headerThirdUrl = INDEX."?coming_from=".$current_file;

$headerFirstClass = "button";
$headerSecondClass = "button-highlighted-green";
$headerThirdClass = "button";

include_once("header_redirects.php"); 
include_once("header.php"); 
?>

<!-- Slider -->
<div id="slider">
	<div class="shell" style="padding-top: 16px;">
		
		<div class="slider-holder">
			<div class="slides" style="top: 0px; padding-bottom: 50px;">
				<div align="center" style="color:#ffffff; margin-bottom: 20px;">
					&nbsp;
				</div>
		    	<div class="slide-bg">
			    	<table border="0px" style="width: 94%;" class="content-table">
			    		<tr>
			    			<td colspan="2" class="publish-content-td" valign="top">
			    				<textarea id="category_textarea" style="height: 100px; margin-top: 25px; font-size: 20px;" tabindex="1"></textarea>
			    			</td>
			    		</tr>
			    		<tr>
							<td align="right" valign="bottom"> <a class="medium-button" style="margin-left:280px; margin-bottom: 5px;" onclick="submitCategory()"><?php echo PUBLISH; ?></a> </td>
			    		</tr>
			    	</table>
		    	</div>
		    	<span class="slide-shadow">&nbsp;</span>
			</div>
			
			<!-- End Slider Navigation -->
		</div>
		
	</div>
</div>
<!-- End Slider -->

<?php include_once('downOptions.php');?>

<script>
	var opts = {
		top: '30%', // Top position relative to parent
		left: '50%' // Left position relative to parent
	};
	var spinnerTarget = document.getElementById('slider');
	var spinner = new Spinner(opts).spin(spinnerTarget);
	spinner.stop();

	if (currentCategory != null) {
		document.getElementById("category_textarea").value = replaceAll("<br />", '\n', currentCategory.name);
	}

	function replaceAll(find, replace, str) {
		return str.replace(new RegExp(find, 'g'), replace);
	}
</script>

</body>
</html>
